package com.wt.admin.util;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.security.exp.IErrorCode;
import com.aizuda.easy.security.exp.impl.BasicException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.regex.Pattern;


public class AssertUtil {


    public static void objIsNull(Object obj, IErrorCode code,Object... args){
        if(ObjectUtil.isEmpty(obj) || ObjectUtil.isNull(obj)){
            throw new BasicException(code, args);
        }
    }

    public static void objIsNotNull(Object obj, IErrorCode code,Object... args){
        if(ObjectUtil.isNotNull(obj) || ObjectUtil.isNotEmpty(obj)){
            throw new BasicException(code,args);
        }
    }

    /**
     * 对象是否相等
     * @param obj1
     * @param obj2
     * @param code
     */
    public static void objEQ(Object obj1, Object obj2,IErrorCode code,Object... args){
        objIsNull(obj1,code);
        objIsNull(obj2,code);
        if(ObjectUtil.equals(obj1,obj2)){
            throw new BasicException(code,args);
        }
    }

    /**
     * 处理字符串
     */
    public static class Str{


        public static void isEmpty(String obj1,IErrorCode code,Object... args){
            if(StrUtil.isBlank(obj1)){
                throw new BasicException(code,args);
            }
        }


        public static void isEQ(String obj1,String obj2,IErrorCode code,Object... args){
            isEmpty(obj1,code);
            isEmpty(obj2,code);
            if(!obj1.equals(obj2)){
                throw new BasicException(code,args);
            }
        }

    }

    /**
     * 处理时间
     */
    public static class Time{
        /**
         * 比较日期 start 是否在 end之前
         * @param start
         * @param end
         * @return
         */
        public static void eqDate(String start,String end,IErrorCode code,Object... args){
            LocalDate date1 = LocalDate.parse(start);
            LocalDate date2 = LocalDate.parse(end);
            if(!date1.isBefore(date2)){
                throw new BasicException(code, args);
            }
        }

        /**
         * 比较时间 start 是否在 end之前
         * @param start
         * @param end
         * @return
         */
        public static void eqDateTime(String start,String end,IErrorCode code,Object... args){
            LocalDateTime date1 = LocalDateTime.parse(start);
            LocalDateTime date2 = LocalDateTime.parse(end);
            if(!date1.isBefore(date2)){
                throw new BasicException(code, args);
            }
        }


        public static void eqDateTime(Date start, Date end, IErrorCode code,Object... args){
            LocalDateTime date1 = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            LocalDateTime date2 = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            if(!date1.isBefore(date2)){
                throw new BasicException(code, args);
            }
        }

        /**
         * 必将两个时间是否相等
         * @param start
         * @param end
         * @param code
         */
        public static void eqDate(Date start, Date end, IErrorCode code,Object... args){
            LocalDate date1 = start.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate date2 = end.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            if(!date1.isBefore(date2)){
                throw new BasicException(code, args);
            }
        }

    }

    /**
     * 处理集合
     */
    public static class List{

        public static void isEmpty(java.util.List list, IErrorCode code,Object... args){
            objIsNull(list,code);
            if(CollUtil.isEmpty(list)){
                throw new BasicException(code, args, args);
            }
        }


        public static void isEmpty(Object[] obj, IErrorCode code,Object... args){
            objIsNull(obj,code);
            isEmpty(Arrays.asList(obj),code,args);
        }

    }

    public static class Network{

        public static void checkIPv4(String ip, IErrorCode code,Object... args){
            Str.isEmpty(ip,code);
            Pattern pattern = Pattern.compile("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
            if(!pattern.matcher(ip).matches()){
                throw new BasicException(code,args);
            }
        }


        public static void checkIPv6(String ip, IErrorCode code,Object... args){
            Str.isEmpty(ip,code);
            Pattern pattern = Pattern.compile("^([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4})$");
            if(!pattern.matcher(ip).matches()){
                throw new BasicException(code,args);
            }
        }

        public static void checkPort(Integer port, IErrorCode code,Object... args){
            objIsNull(port,code);
            if(!NetUtil.isValidPort(port)){
                throw new BasicException(code,args);
            }
        }

    }

    public static class Boolean{

        public static void isTrue(boolean v, IErrorCode code,Object... args){
            if(v){
                throw new BasicException(code,args);
            }
        }

        public static void isFalse(boolean v, IErrorCode code,Object... args){
            if(!v){
                throw new BasicException(code,args);
            }
        }

    }

}
